{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "caf2600d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bb4e79a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = pd.read_csv('digit-recognizer/train.csv')\n",
    "train_features = train_data.drop('label',axis=1)/255.0\n",
    "# train_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "12eb9cb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = pd.read_csv('digit-recognizer/test.csv')\n",
    "test_features = test_data/255.0\n",
    "# test_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7b07b65f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = np.array(train_features.values,dtype=np.float)\n",
    "test_dataset = np.array(test_features.values,dtype=np.float)\n",
    "train_labels = np.array(train_data.label.values,dtype=np.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a0510f64",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = train_dataset.reshape(train_dataset.shape[0], 28, 28, 1)\n",
    "test_dataset = test_dataset.reshape(test_dataset.shape[0], 28, 28, 1)\n",
    "train_dataset = np.pad(train_dataset, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
    "test_dataset = np.pad(test_dataset, ((0,0),(2,2),(2,2),(0,0)), 'constant')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.models.Sequential()\n",
    "#Layer 1\n",
    "#Conv Layer 1\n",
    "model.add(keras.layers.Conv2D(filters = 6, \n",
    "                 kernel_size = 5, \n",
    "                 strides = 1, \n",
    "                 activation = 'relu', \n",
    "                 input_shape = (32,32,1)))\n",
    "#Pooling layer 1\n",
    "model.add(keras.layers.MaxPooling2D(pool_size = 2, strides = 2))\n",
    "#Layer 2\n",
    "#Conv Layer 2\n",
    "model.add(keras.layers.Conv2D(filters = 16, \n",
    "                 kernel_size = 5,\n",
    "                 strides = 1,\n",
    "                 activation = 'relu',\n",
    "                 input_shape = (14,14,6)))\n",
    "#Pooling Layer 2\n",
    "model.add(keras.layers.MaxPooling2D(pool_size = 2, strides = 2))\n",
    "#Flatten\n",
    "model.add(keras.layers.Flatten())\n",
    "#Layer 3\n",
    "#Fully connected layer 1\n",
    "model.add(keras.layers.Dense(units = 120, activation = 'relu'))\n",
    "#Layer 4\n",
    "#Fully connected layer 2\n",
    "model.add(keras.layers.Dense(units = 84, activation = 'relu'))\n",
    "#Layer 5\n",
    "#Output Layer\n",
    "model.add(keras.layers.Dense(units = 10, activation = 'softmax'))\n",
    "model.compile(optimizer=tf.keras.optimizers.Adam(0.01),\n",
    "              loss = 'sparse_categorical_crossentropy',\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "aee42730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 29399 samples, validate on 12601 samples\n",
      "Epoch 1/10\n",
      "29399/29399 - 5s - loss: 0.2037 - accuracy: 0.9367 - val_loss: 0.0998 - val_accuracy: 0.9713\n",
      "Epoch 2/10\n",
      "29399/29399 - 2s - loss: 0.0864 - accuracy: 0.9741 - val_loss: 0.0737 - val_accuracy: 0.9791\n",
      "Epoch 3/10\n",
      "29399/29399 - 3s - loss: 0.0817 - accuracy: 0.9766 - val_loss: 0.1033 - val_accuracy: 0.9736\n",
      "Epoch 4/10\n",
      "29399/29399 - 3s - loss: 0.0656 - accuracy: 0.9814 - val_loss: 0.1129 - val_accuracy: 0.9729\n",
      "Epoch 5/10\n",
      "29399/29399 - 3s - loss: 0.0695 - accuracy: 0.9811 - val_loss: 0.1083 - val_accuracy: 0.9772\n",
      "Epoch 6/10\n",
      "29399/29399 - 3s - loss: 0.0720 - accuracy: 0.9820 - val_loss: 0.1736 - val_accuracy: 0.9675\n",
      "Epoch 7/10\n",
      "29399/29399 - 3s - loss: 0.0674 - accuracy: 0.9823 - val_loss: 0.1243 - val_accuracy: 0.9736\n",
      "Epoch 8/10\n",
      "29399/29399 - 3s - loss: 0.0604 - accuracy: 0.9849 - val_loss: 0.0982 - val_accuracy: 0.9770\n",
      "Epoch 9/10\n",
      "29399/29399 - 2s - loss: 0.0667 - accuracy: 0.9840 - val_loss: 0.1144 - val_accuracy: 0.9757\n",
      "Epoch 10/10\n",
      "29399/29399 - 3s - loss: 0.0608 - accuracy: 0.9847 - val_loss: 0.0925 - val_accuracy: 0.9777\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x192a9ebc0f0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#训练\n",
    "model.fit(train_dataset, train_labels, epochs=10, batch_size=64, validation_split=0.3,verbose=2,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28000,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds=model.predict(test_dataset)#使用模型对测试集进行预测\n",
    "preds=np.argmax(preds, axis=1)#返回每行可能性最大的值\n",
    "print(preds.shape)\n",
    "preds[900]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6624616c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x19331ac9588>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAA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T39edtfs5vZREl/lvRdSfskbZW01N13trWRHGa2R9Jcd6/9AxhmdqOkY5L+68TUWmb275KOuPtD2X+UU9z9px3S2wM6xWm8K+otb5rxf1aNz10rpz9vRh1H9msl7Xb3d939uKRfS1pYQx8dz91flHTkpMULJa3O7q/W6D+WtsvprSO4+5C778jufyjpxDTjtT53ib7aoo6wXyzpr2Me71Nnzffukn5vZtvNrK/uZsYx/cQ0W9nttJr7OVnhNN7tdNI04x3z3DUz/XlZdYR9vKlpOmn873p3/5akf5L0o+x0FY1ZKWmWRucAHJL0izqbyaYZf0bSPe4+UmcvY43TV1uetzrCvk9Sz5jHMyQdqKGPcbn7gex2WNJzGn3Z0UkOnphBN7sdrrmfL7j7QXf/zN0/l/RL1fjcZdOMPyNpjbs/my2u/bkbr692PW91hH2rpMvM7OtmNknSEknP19DHl5jZ5OyNE5nZZEnz1XlTUT8vaVl2f5mk39XYy9/olGm886YZV83PXe3Tn7t7238k3aLRd+TfkfSvdfSQ09dMSa9nP2/V3ZuktRo9rftEo2dEP5B0vqSNkt7Obqd2UG//rdGpvd/QaLC6a+rt2xp9afiGpNeyn1vqfu4SfbXleePjskAQfIIOCIKwA0EQdiAIwg4EQdiBIAg7EARhB4L4f+q3p6HzEkgBAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sample = np.array(train_features.iloc[900, :])\n",
    "sample = sample.reshape([28,28])\n",
    "plt.imshow(sample, cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3b0554df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#preds=np.array(model.predict_classes(test_dataset),dtype=np.int32)\n",
    "preds=model.predict(test_dataset)\n",
    "preds=np.argmax(preds, axis=1)\n",
    "submission = pd.DataFrame({\n",
    "    'ImageId': [i for i in range(1,28001)],\n",
    "    'Label':preds\n",
    "})\n",
    "submission.to_csv('submission.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
